package com.example.unisys.controller;


import com.example.unisys.common.Response;
import com.example.unisys.common.STATUS;
import com.example.unisys.entity.Course;
import com.example.unisys.entity.MainCourse;
import com.example.unisys.entity.SubCourse;
import com.example.unisys.entity.User;
import com.example.unisys.service.CourseService;
import com.example.unisys.service.MainCourseService;
import com.example.unisys.service.SubCourseService;
import com.example.unisys.service.UserService;
import com.example.unisys.utils.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/apis/v1/repository", produces = "application/json; charset=utf-8")
public class RepositoryController {
    @Autowired
    CourseService courseService;

    @Autowired
    MainCourseService mainCourseService;

    @Autowired
    UserService userService;

    @Autowired
    SubCourseService subCourseService;

    /*
     * 获取学校课程
     * 不知道用到了没
     * */
    @RequestMapping(value = "/courses/searchMainCourse", method = {RequestMethod.GET})
    public Response getCoursesBySearch(@RequestParam("user_type") Integer user_type, @RequestParam("main_school") String main_school, @RequestParam("sub_school") String sub_school, @RequestParam("limit") int limit, @RequestParam("page") int page, @RequestParam("searchText") String searchText, @RequestParam("searchType") int searchType) {
        Map<String, Object> hs1 = new HashMap<>();
        hs1.put("main_school", main_school);
        hs1.put("searchText", searchText.trim());
        if (user_type == 3) hs1.put("sub_school", sub_school);
//        if (Tools.isNumeric(searchText)) hs1.put("course_id", Integer.parseInt(searchText));
//        else hs1.put("course_name", searchText.trim());
//        if (!main_school.equals(sub_school)) hs1.put("sub_school", sub_school);
        List<Course> ls = courseService.getMainCourseBySearch(hs1);
        return Response.builder().status(STATUS.SUCCESS).data(ls).total(ls.size()).build();
    }

    /*
     *  获取课程
     * */
    @RequestMapping(value = "/courses", method = {RequestMethod.GET})
    public Response getCourses(@CookieValue("Admin-Token") String token, @RequestParam("limit") int limit, @RequestParam("page") int page, @RequestParam("searchType") int searchType) {
        int newlimit = (page - 1) * limit;
        int pagesize = limit;
        String username = JWTUtil.getUsername(token);
        User user = userService.getInfo(username);
        if (user.getUser_type() == 1) {
            List<Course> ls = courseService.getAllCourse(newlimit, pagesize);
            return Response.builder().status(STATUS.SUCCESS).data(ls).total(courseService.getRepoCourseSum()).build();
        } else if (user.getUser_type() == 2) {
            List<Course> ls = courseService.getValidCourse(newlimit, pagesize);
            List<MainCourse> mainCourses = mainCourseService.getBySchool(user.getMain_school());
            ls.forEach(cs->{
                cs.setIs_shift(0);
                mainCourses.forEach(mainCourse->{
                    if(mainCourse.getCourse_id().equals(cs.getCourse_id()))cs.setIs_shift(1);
                });
            });
            return Response.builder().status(STATUS.SUCCESS).data(ls).total(courseService.getValidCourseSum()).build();
//            for (Course cs : ls) {
//                cs.setIs_main(0);
//                for (MainCourse ms : maincourses) {
//                    // System.out.println("mainCourse id is "+ms.getCourse_id()+"  course id is "+cs.getCourse_id());
//                    if (ms.getCourse_id().equals(cs.getCourse_id()))
//                        cs.setIs_main(1);
//                }
//            }
//
//            res.put("code", 20000);
//            res.put("data", ls);
//            res.put("total", courseService.getValidCourseSum());
//            return res;
        } else if (user.getUser_type() == 3) {
            List<MainCourse> ls = mainCourseService.getMainValidCourse(user.getMain_school(), newlimit, pagesize);
            List<SubCourse> subCourses = subCourseService.getBySchool(user.getMain_school(), user.getSub_school(), 0, 1000);
            ls.forEach(cs->{
                cs.setIs_valid(0);
                subCourses.forEach(subCourse->{
                    if(subCourse.getCourse_id().equals(cs.getCourse_id()))cs.setIs_valid(1);
                });
            });
//            for (MainCourse cs : ls) {
//                cs.setIs_sub(0);
//                for (SubCourse ss : subcourses) {
//                    if (ss.getCourse_id().equals(cs.getCourse_id()))
//                        cs.setIs_sub(1);
//                }
//            }
            return Response.builder().status(STATUS.SUCCESS).data(ls).total(mainCourseService.getMainValidCourseSum(user.getMain_school())).build();

//            res.put("code", 20000);
//            res.put("data", ls);
//            res.put("total", mainCourseService.getMainValidCourseSum(user.getMain_school()));
//            return res;
        } else {
            return Response.builder().status(STATUS.BAD_REQUEST).build();
        }
    }

}
